/*
 * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netease.financial.base.share.activity;

import android.app.Activity;
import android.os.Bundle;

import com.netease.financial.R;
import com.netease.financial.common.config.NfsConfig;
import com.netease.financial.common.util.ToastUtils;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.constant.WBConstants;

/**
 * 该类演示了第三方应用如何通过微博客户端分享文字、图片、视频、音乐等。
 * 执行流程： 从本应用->微博->本应用
 *
 * @author SINA
 * @since 2013-10-22
 */
public class SinaWeiboEntryActivity extends Activity implements IWeiboHandler.Response {
//    private static final String TAG = SinaWeiboEntryActivity.class.getSimpleName();

//    public static final String KEY_SHARE_TYPE = "key_share_type";
//    public static final int SHARE_CLIENT = 1;
//    public static final int SHARE_ALL_IN_ONE = 2;

    /**
     * 微博微博分享接口实例
     */
    private IWeiboShareAPI mWeiboShareAPI = null;

//    private int mShareType = SHARE_CLIENT;

    /**
     * @see {@link Activity#onCreate}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        TextView view = new TextView(this);
//        view.setText("SinaWeiboEntryActivity");
//        setContentView(view);


//        mShareType = getIntent().getIntExtra(KEY_SHARE_TYPE, SHARE_CLIENT);

        // 创建微博分享接口实例
        mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, NfsConfig.getSinaWeiboAppKey());

        // 注册第三方应用到微博客户端中，注册成功后该应用将显示在微博的应用列表中。
        // 但该附件栏集成分享权限需要合作申请，详情请查看 Demo 提示
        // NOTE：请务必提前注册，即界面初始化的时候或是应用程序初始化时，进行注册
        mWeiboShareAPI.registerApp();

        // 从当前应用唤起微博并进行分享后，返回到当前应用时，需要在此处调用该函数
        // 来接收微博客户端返回的数据；执行成功，返回 true，并调用
        // {@link IWeiboHandler.Response#onResponse}；失败返回 false，不调用上述回调
        mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
    }

    /**
     * 接收微客户端博请求的数据。
     * 当微博客户端唤起当前应用并进行分享时，该方法被调用。
     * <p/>
     * //     * @param baseRequest 微博请求数据对象
     *
     * @see {@link IWeiboShareAPI#handleWeiboRequest}
     */
    @Override
    public void onResponse(BaseResponse baseResp) {
        int result;
        switch (baseResp.errCode) {
            case WBConstants.ErrorCode.ERR_OK:
                result = R.string.errcode_success;
                break;
            case WBConstants.ErrorCode.ERR_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case WBConstants.ErrorCode.ERR_FAIL:
                result = R.string.errcode_deny;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }
        ToastUtils.showShort(this, result);
        finish();
    }

}
